import routerMap from "../routerMap";
import Vue from 'vue'
import ViewUI from 'view-design';

Vue.use(ViewUI);
import 'view-design/dist/styles/iview.css';
import '~/assets/css/common.css'

export default ({app,store,redirect})=>{
  app.router.beforeEach((to,from,next)=>{
    //console.log(666666666666,app.store.getters.userInfo)
    ViewUI.LoadingBar.start();
    let userInfo = localStorage.getItem("userInfo");
    // 登录拦截
    if(!routerMap.includes(to.path)){
      if (userInfo){
        store.dispatch("setLogin",userInfo).then(userInfo => {
          next();
        }).catch(error => {
          localStorage.removeItem("userInfo");
          next({path:'/',replace:true});
        });
      }else {
        // 未登录跳转首页
        next("/");
      }
    }else{
      next();
    }
  });

  app.router.afterEach((to,from)=>{
    ViewUI.LoadingBar.finish();
  });

  app.router.onError(error => {
    console.log('%c ! router error','background:#000;color:#bada55',error);
    ViewUI.LoadingBar.error();
  });
};
